package com.sww.pj.sys.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sww.pj.common.pojo.JsonResult;
import com.sww.pj.sys.dao.IVerifyCodeGen;
import com.sww.pj.sys.pojo.SysUser;
import com.sww.pj.sys.pojo.VerifyCode;
import com.sww.pj.sys.service.SysUserService;
import com.sww.pj.sys.service.impl.SimpleCharVerifyCodeGenImpl;

@RestController
@RequestMapping("/user/")
public class SysUserController {
	
	

	@Autowired
	private SysUserService sysUserService;
	
	//把数据放到页面
	@RequestMapping("doFindPageObjects")
	public JsonResult doFindPageObjects(String username,Long pageCurrent) {
		return new JsonResult(sysUserService.findPageObjects(username,pageCurrent));
	}
	
	//删除操作 
	@RequestMapping("doDeleteObjects")
	public JsonResult doDeleteObjects(Integer... ids){
		sysUserService.deleteObjects(ids);
		return new JsonResult("删除成功！");
	}
	
	//添加操作
	@RequestMapping("doSaveObjects")
	public JsonResult doSaveObjects(SysUser entity,Integer[]roleIds) {
		sysUserService.saveObjects(entity, roleIds);
		return new JsonResult("添加成功！！");
	}
	
	//修改操作
	@RequestMapping("doUpdateObjects")
	public JsonResult doUpdateObjects(SysUser entity,Integer[]roleIds) {
		sysUserService.updateObjects(entity, roleIds);
		return new JsonResult("修改成功！！");
	}
	//修改的那个查询
	@RequestMapping("doFindObjectByIds")
	public JsonResult doFindObjectByIds(Integer id) {
		return new JsonResult(sysUserService.findObjectByIds(id));
	}
	
private Map<String,String> vcode = new HashMap<>();
	
	@RequestMapping("doLogin")
      public JsonResult doLogin(String username,String code,String password,boolean isRememberMe,String uuid){
         String   inputCode=vcode.get(uuid);
         if(inputCode !=null) {
        	 if(!code.equalsIgnoreCase(inputCode)) {
        		 throw new RuntimeException("验证码错误");
        	 }
         }
		
		
		//1.获取Subject对象
              Subject subject=SecurityUtils.getSubject();
              UsernamePasswordToken token=new UsernamePasswordToken();
              token.setUsername(username);
              token.setPassword(password.toCharArray());
              if(isRememberMe) {
                  token.setRememberMe(true);
           }
              subject.login(token);
              return new JsonResult("login ok");

      }
	
	
	
	
	@RequestMapping("/verifyCode")
	public void verifyCode(HttpServletRequest request, HttpServletResponse response,String uuid) {
	IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
	try {
	//设置长宽
	VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);	
	String code = verifyCode.getCode();
	//将VerifyCode绑定session
	request.getSession().setAttribute("VerifyCode", code);
	vcode.put(uuid, code);
	//设置响应头
	response.setHeader("Pragma", "no-cache");
	//设置响应头
	response.setHeader("Cache-Control", "no-cache");
	//在代理服务器端防止缓冲
	response.setDateHeader("Expires", 0);
	//设置响应内容类型
	response.setContentType("image/jpeg");
	response.getOutputStream().write(verifyCode.getImgBytes());
	response.getOutputStream().flush();
	} catch (IOException e) {
	}
	}
	
	
}
